package com.glxy.controller;

import com.glxy.dao.impl.RoleDaoImpl;
import com.glxy.pojo.Role;
import com.glxy.pojo.User;
import com.glxy.service.impl.LoginServiceImpl;
import com.glxy.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Date;
import java.time.LocalDateTime;
import java.util.ArrayList;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        String action = request.getParameter("action");
        UserServiceImpl userService = new UserServiceImpl();

        if (action.equals("list")){
            System.out.println("UserList");

            int pageNo = 1;
            String pageNos = request.getParameter("pageNo");

            if (pageNos != null){
                pageNo = Integer.parseInt(pageNos);
            }

            int pageSize = 5;

            int totalCount = userService.totalCount();
            int totalPage = 0;

            if (totalCount%pageSize == 0) {
                totalPage = totalCount/pageSize;
            }else{
                totalPage = totalCount/pageSize + 1;
            }

            int prev = pageNo;
            int next = pageNo;

            if (pageNo > 1){
                prev--;
            }
            if (pageNo < totalPage) {
                next++;
            }

            ArrayList<User> list = userService.findAllUserPages(pageSize, pageNo);
            ArrayList<Role> roles = new RoleDaoImpl().findAll();

            request.setAttribute("localTime", LocalDateTime.now().getYear());
            request.setAttribute("totalPage", totalPage);
            request.setAttribute("prev", prev);
            request.setAttribute("next", next);
            request.setAttribute("pageNo", pageNo);
            request.setAttribute("list", list);
            request.setAttribute("roles", roles);
            request.getRequestDispatcher("userList.jsp").forward(request, response);

        }else if (action.equals("search")){
            String userName = request.getParameter("userName");

            ArrayList<User> list = userService.findUser(userName);

            request.setAttribute("list",list);
            request.getRequestDispatcher("userList.jsp").forward(request,response);

        }else if(action.equals("add")){

            User user = new User();

            String pwd = request.getParameter("userpassword");
            String rePwd = request.getParameter("userRemi");

            if (pwd.equals(rePwd)){
                user.setId(Long.valueOf(request.getParameter("userId")));
                user.setUserCode(request.getParameter("userCode"));
                user.setUserName(request.getParameter("userName"));
                user.setUserPassword(pwd);
                if (request.getParameter("gender").equals("man")){
                    user.setGender(1);
                }else{
                    user.setGender(2);
                }

                user.setBirthday(Date.valueOf(request.getParameter("date")));
                user.setPhone(request.getParameter("userphone"));
                user.setAddress(request.getParameter("userAddress"));
                user.setUserRole((long)1);

                userService.insertUser(user);
            }else{
                System.out.println("两次密码不一致");
            }

            request.getRequestDispatcher("UserServlet?action=list").forward(request,response);

        }else if(action.equals("view")){

            User user = userService.findUser(request.getParameter("name")).get(0);
            ArrayList<Role> roles = new RoleDaoImpl().findAll();


            request.setAttribute("user", user);
            request.setAttribute("roles", roles);

            request.getRequestDispatcher("userView.jsp").forward(request, response);

        }else if(action.equals("update")) {
            String name = request.getParameter("name");

            User user = userService.findUser(name).get(0);

            request.setAttribute("user", user);
            request.getRequestDispatcher("userUpdate.jsp").forward(request, response);

        }else if (action.equals("updateSave")){
            User user = userService.findUser(request.getParameter("oriName")).get(0);

            user.setUserName(request.getParameter("userName"));
            user.setBirthday(Date.valueOf(request.getParameter("data")));
            user.setPhone(request.getParameter("userphone"));
            user.setAddress(request.getParameter("userAddress"));
            user.setUserRole((long)1);

            userService.update(user);

            request.getRequestDispatcher("UserServlet?action=list").forward(request,response);

        }else if(action.equals("delete")){
            System.out.println(request.getParameter("name"));

            String name = request.getParameter("name");

            User user = userService.findUser(name).get(0);

            userService.delete(user.getId());

            request.getRequestDispatcher("UserServlet?action=list").forward(request, response);
        }else {
            System.out.println("Error");
        }


    }
}
